<template>
  <el-dialog
    :title="title"
    width="600px"
    center
    :visible.sync="dialogFormVisible"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <el-form-item status-icon label="原密码" prop="old_password">
        <el-input type="password" v-model="form.old_password " show-password autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password"  v-model="form.password" show-password autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <template slot="label">
          <label for="password" class="el-form-item__label is_required">
            确认密码
          </label>
        </template>
        <el-input type="password" v-model="form.checkPass" show-password autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">返回</el-button>
      <el-button v-no-more-click:3000 type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updatePwd } from "@/api/user";

export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      dialogFormVisible: false,
      title: "修改密码",
      form: {},
      rules: {
        old_password: [
          { required: true, message: "请输入原密码", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods: {
    showDialog() {
      this.dialogFormVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var obj = {
            old_password: this.form.old_password,
            new_password: this.form.password,
           comfirm_new_password:this.form.checkPass

          };
          updatePwd( obj)
            .then(res => {
              if (res.code == 200) {
                this.$message({
                  message: "修改成功,请重新登录",
                  type: "success"
                });
                this.dialogFormVisible = false
              
                  this.$router.push({
                    path: "/login"
                  })
              
                this.$store.commit("delToken")
                this.$cookies.remove("token")
                this.$store.commit("delUser")
                this.$cookies.remove("user")

              } else {
                this.$message({
                  message: res.message,
                  type: "error"
                });
              }
            })
            .catch(err => {
              this.$message({
                message: err,
                type: "error"
              });
            });
        } else {
          return false;
        }
      });
    },

    handleClose() {
      this.resetForm("ruleForm");
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.form = {};
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style>
.is_required::before {
    content: '*';
    color: #F56C6C;
    margin-right: 4px;
}
</style>
